package com.mark.web.stars.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mark.common.Constant;
import com.mark.common.ServerResponse;
import com.mark.web.stars.entity.po.Memo;
import com.mark.web.stars.entity.vo.MemoVo;
import com.mark.web.stars.enums.MemoEnum;
import com.mark.web.stars.mapper.MemoMapper;
import com.mark.web.stars.service.IMemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 《星运》-备忘录的service业务实现
 * @Author: Mark
 * @CreateDate: 2019/3/28 21:32
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Service
public class MemoServiceImpl implements IMemoService {

    private final MemoMapper memoMapper;

    @Autowired
    public MemoServiceImpl(MemoMapper memoMapper) {
        this.memoMapper = memoMapper;
    }

    @Override
    public ServerResponse<String> add(Memo memo) {
        if(memo.getEnableStatus() == null){
            memo.setEnableStatus((short) MemoEnum.CREATE.getState());
        }
        if(memo.getExpectTime() == null){
            memo.setExpectTime(new Date());
        }
        int effectNum = memoMapper.insert(memo);
        if(effectNum != Constant.SUCCESS){
            ServerResponse.createByErrorMessage("保存信息失败！");
        }
        return ServerResponse.createBySuccessMessage("添加备忘录成功！");
    }

    @Override
    public ServerResponse<PageInfo<MemoVo>> getMemoListBySearch(Map<String, Object> paramMap) {
        //取出请求中的参数
        Date searchStartTimeDate = null;
        Date searchEndTimeDate  = null;
        int pageNum = (int) paramMap.get("pageNum");
        int pageSize = (int) paramMap.get("pageSize");
        int status = (int) paramMap.get("status");
        Object searchTimeObj = paramMap.get("searchTime");

        //如果搜索时间存在，则初始化搜索时间的参数
        if( searchTimeObj != null){
            long searchStartTime = (long) searchTimeObj;
            long searchEndTime = getSearchEndTime(searchStartTime);
            searchStartTimeDate = new Date(searchStartTime);
            searchEndTimeDate = new Date(searchEndTime);
        }
        int currentUserId = (int) paramMap.get("currentUserId");

        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> map = Maps.newHashMap();
        map.put("currentUserId",currentUserId);
        map.put("searchStartDate",searchStartTimeDate);
        map.put("searchEndDate",searchEndTimeDate);
        map.put("status",status);
        List<Memo> memoList =  memoMapper.selectMemosByCondition(map);

        List<MemoVo> memoVoList = Lists.newArrayList();
        memoList.forEach(memo -> memoVoList.add(new MemoVo(memo)));

        PageInfo<MemoVo> result = new PageInfo<>(memoVoList);
        result.setTotal(new PageInfo<>(memoList).getTotal());
        return ServerResponse.createBySuccess(result);
    }

    /**
     * 根据起始时间获取当天的24点时间戳
     * @param searchStartTime ：搜索开始戳
     * @return ：当前的结束时间戳
     */
    private long getSearchEndTime(long searchStartTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(searchStartTime));
        calendar.set(Calendar.HOUR_OF_DAY,24);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTimeInMillis();
    }

    @Override
    public ServerResponse<String> edit(Memo memo) {
        int effectNum = memoMapper.updateByPrimaryKeySelective(memo);
        if(effectNum != Constant.SUCCESS){
            ServerResponse.createByErrorMessage("更新备忘录信息失败！");
        }
        return ServerResponse.createBySuccessMessage("更新备忘录成功！");
    }

    @Override
    public ServerResponse<String> finish(Integer id, int currentUserId) {
        int effectNum = memoMapper.updateMemoStatus(id,currentUserId,MemoEnum.FINISH.getState());
        if(effectNum != Constant.SUCCESS){
           return ServerResponse.createByErrorMessage("更新备忘录信息失败！");
        }
        return ServerResponse.createBySuccessMessage("备忘录已更改为完成状态！");
    }

    @Override
    public ServerResponse<String> delete(Integer id, int currentUserId) {
        int effectNum = memoMapper.delete(id, currentUserId);
        if(effectNum != Constant.SUCCESS){
            return ServerResponse.createByErrorMessage("删除备忘录失败！");
        }
        return ServerResponse.createBySuccessMessage("删除删除成功");
    }


}
